package jiinn.controllers;

import java.util.List;

import jiinn.interfaces.MemberService;
import jiinn.models.MemberVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

@Controller
@SessionAttributes("loginmno")
public class IndexController {
	
	@Autowired
	private MemberService memberService;
	
	@RequestMapping(value="/index", method=RequestMethod.GET)
	public String index(Model model) throws Exception {
		return "index";
	}
	@RequestMapping(value="/index", method=RequestMethod.POST)
	public String index(Model model, @RequestParam List<String> acc) throws Exception {
		return "index";
	}
	
	@RequestMapping(value="/login", method=RequestMethod.POST)
	public String login(@ModelAttribute MemberVO m, ModelMap model) throws Exception {
		String viewPage = "redirect:/loginfail.pen";
		String mno = memberService.loginMember(m);
		if (mno != null) {
			model.put("loginmno", mno);
			viewPage = "redirect:/index.pen";
		}		
		return viewPage;
	}
	
	@RequestMapping("/logout")
	public String logout(SessionStatus sess) throws Exception {
		sess.setComplete();
		return "redirect:/index.pen";
	}
	
	@RequestMapping("/loginfail")
	public String loginfail() throws Exception {		
		return "error/loginfail";
	}	
}
